package com.example.book.controller;

import com.example.book.constant.Constants;
import com.example.book.enums.ResultStatus;
import com.example.book.mapper.BookMapper;
import com.example.book.model.*;
import com.example.book.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.awt.print.Book;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-11-14
 * Time: 21:47
 */
@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;//注入依赖

//  接口不建议使用，后期可能会废弃
//    @RequestMapping("/getBookList")
//    //mock数据模拟造假数据
//    public List<BookInfo> getBookList(){
//        //BookService bookService = new BookService();
//        List<BookInfo> bookInfos =bookService.getBookList();
//        return bookInfos;
//    }

    @RequestMapping (value = "/addBook",produces = "application/json")
    public String addBook(BookInfo bookInfo){
        log.info("添加图书，接收到的参数：bookInfo {}",bookInfo);
        if(
                !StringUtils.hasLength(bookInfo.getBookName())
            ||  !StringUtils.hasLength(bookInfo.getAuthor())
            ||  bookInfo.getCount() == null
            ||  bookInfo.getPrice() == null
            ||  !StringUtils.hasLength(bookInfo.getPublish())
            ||  bookInfo.getStatus() == null
        ){
            return "输入的参数不合法";
        }
        try {
            Integer result = bookService.insertBook(bookInfo);
            if(result > 0){
                return "";
            }
        }catch (Exception e){
            log.error("添加的图书异常，e" , e);//手动写一个异常报错
        }
        return "添加失败";

    }

    /*
    * 查询图书列表*/
    @RequestMapping("/getBookListByPage")
//    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest){
    public Result<PageResult<BookInfo>> getBookListByPage(PageRequest pageRequest, HttpSession session){
        log.info("查询图书列表，请求参数为pageRequest:{}",pageRequest);
//         UserInfo user_session_key = (UserInfo)session.getAttribute("user_session_key");//对代码中的常量进行设置

//      写好拦截器之后就可以不要下面的代码了

//         UserInfo loginUserInfo = (UserInfo)session.getAttribute(Constants.USER_SESSION_KEY);
//         if(loginUserInfo == null || loginUserInfo.getId() <= 0){
//           //静态方法直接调用
//             return Result.nologin();
//         }

//         说明登录成功
         PageResult<BookInfo> bookList = bookService.getBookListByPage(pageRequest);
         return Result.success(bookList);
        //拿到PageRequest这个对象
        //再通过Service层去调用方法
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
        log.info("根据Id查询图书信息" + bookId);
        return bookService.queryBookById(bookId);
    }

    /*
    * 更新图书*/
    @RequestMapping(value = "/updateBook",produces = "application/json")
    public String updateBook(BookInfo bookInfo){
        log.info("更新图书，bookinfo：{}", bookInfo);
        try{
            Integer result = bookService.updateBook(bookInfo);
            if(result > 0){
                return "";
            }
            return "内部错误，请联系管理员";
        }catch (Exception e){
            log.info("更新图书失败，e",e);
//            return "更新图书失败：" + e.getMessage();//不打印sql给前端，影响用户体验
            return "更新图书失败";
        }
    }
    @RequestMapping(value = "/deleteBook",produces = "application/json")
    public String deleteBook(Integer bookId){
        log.info("删除图书，bookId：{}", bookId);
        try{
            Integer result = bookService.deleteBook(bookId);
            if(result > 0){
                return "";
            }
            return "内部错误，请联系管理员";
        }catch (Exception e){
            log.info("更新图书失败，e",e);
//            return "更新图书失败：" + e.getMessage();//不打印sql给前端，影响用户体验
            return "更新图书失败";
        }
    }

    @RequestMapping(value = "/batchDeleteBook",produces = "application/json")
    public String batchDelete(@RequestParam List<Integer> ids){
        log.info("批量删除图书, ids{}",ids);
        Integer result = bookService.batchDeleteBookByIds(ids);
        if(result > 0){
            return "";
        }
        return "删除失败，请联系管理员";
    }



}
